package space.xxhui.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by hui on 2017/7/18.
 */
@Controller
public class UploadController {
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public @ResponseBody String upload(MultipartFile file, HttpServletRequest request){

        try {
            String rootPath = request.getSession().getServletContext().getRealPath("/");
            //String resourseLocation = (String) request.getSession().getServletContext().getResourcePaths("/").toArray()[1];
            System.out.println("根目录路径："+rootPath);
            FileUtils.writeByteArrayToFile(new File(rootPath+"WEB-INF/imagez/"+file.getOriginalFilename()),file.getBytes());
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
    }
}
